﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenRPOSBackOffice
{
    public partial class AddOptionGroupDialog : ChildWindow
    {

        public AddOptionGroupDialog()
        {
            InitializeComponent();
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Determine if a non-active record with this name already exists
            await Globals.db.GetOptionGroupObjects("Active=0 and Name='" + tbOptionGroupName.Text.Trim() + "'");
            if (Globals.db.OptionGroupRecords.Count > 0)
            {
                Globals.db.OptionGroupRecords[0].col_Active = true;
                Globals.db.OptionGroupRecords[0].bFilterActive = true;
                Globals.db.OptionGroupRecords[0].col_Name = tbOptionGroupName.Text.Trim();
                Globals.db.OptionGroupRecords[0].bFilterName = true;
                Globals.db.OptionGroupRecords[0].col_Exclusive = (cbExclusive.IsChecked == true);
                Globals.db.OptionGroupRecords[0].bFilterExclusive = true;
                await Globals.db.UpdateOptionGroupObject(Globals.db.OptionGroupRecords[0]);
            }
            else
            {
                // Create a new menu category with this name
                ServiceReferenceOpenRPOS.OptionGroup og = new ServiceReferenceOpenRPOS.OptionGroup();
                og.col_Name = tbOptionGroupName.Text.Trim();
                og.bFilterName = true;
                og.col_Active = true;
                og.bFilterActive = true;
                og.col_Exclusive = (cbExclusive.IsChecked == true);
                og.bFilterExclusive = true;
                await Globals.db.AddOptionGroupObject(og);
            }
            // Re-init the display
            Globals.menuItems.initOptionGroups(true);
            this.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbOptionGroupName_KeyDown(object sender, KeyEventArgs e)
        {
            // Call the OK button handlers if "Enter" is pressed
            if (e.Key == Key.Enter)
                btnOK_Click(sender, e);
        }

    }
}

